﻿namespace WcfJsHoster
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Xml;

    public class WebConfigInfo
    {
        public WebConfigInfo(string webconfig)
        {
            this.list = new List<ServiceInfo>();
            XmlDocument document = new XmlDocument();
            document.Load(webconfig);
            System.Xml.XmlNode node = document.SelectSingleNode("configuration");
            if (node != null)
            {
                foreach (System.Xml.XmlNode node2 in node.ChildNodes)
                {
                    XmlElement element = node2 as XmlElement;
                    if ((element != null) && (element.Name.ToLower() == "system.servicemodel"))
                    {
                        foreach (System.Xml.XmlNode node3 in node2.ChildNodes)
                        {
                            XmlElement element2 = node3 as XmlElement;
                            if ((element2 != null) && (element2.Name.ToLower() == "services"))
                            {
                                foreach (System.Xml.XmlNode node4 in node3.ChildNodes)
                                {
                                    XmlElement element3 = node4 as XmlElement;
                                    if ((element3 != null) && (element3.Name.ToLower() == "service"))
                                    {
                                        ServiceInfo item = new ServiceInfo {
                                            ServiceName = element3.GetAttribute("name")
                                        };
                                        foreach (System.Xml.XmlNode node5 in node4.ChildNodes)
                                        {
                                            XmlElement element4 = node5 as XmlElement;
                                            if ((element4 != null) && (element4.Name.ToLower() == "endpoint"))
                                            {
                                                string attribute = element4.GetAttribute("address");
                                                string str2 = element4.GetAttribute("binding");
                                                string str3 = element4.GetAttribute("contract");
                                                if (!(!(str2.ToLower() == "webhttpbinding") || string.IsNullOrEmpty(str3)))
                                                {
                                                    EndPointInfo info2 = new EndPointInfo {
                                                        Address = attribute,
                                                        ContractName = str3
                                                    };
                                                    item.EndPoints.Add(info2);
                                                }
                                            }
                                        }
                                        if (!(string.IsNullOrEmpty(item.ServiceName) || (item.EndPoints.Count <= 0)))
                                        {
                                            this.list.Add(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public List<ServiceInfo> list { get; set; }
    }
}

